package com.nick.job.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * 统一时间日期获取处理类<br/>
 * 主要考虑统一获取日期，方便统一和外围系统联调及其他时间时，引起的取系统时间引起的不一致问题
 * 
 * @author dcliu
 */
public class DateUtils {
	
	private static SimpleDateFormat simpleDateFormat;
	
    public static final String AMR_NOMAL_DATE_FORMAT      = "yyyy/MM/dd";
    public static final String AMR_NOMAL_DATE_FORMAT2     = "yyyy-MM-dd";
    public static final String AMR_NOMAL_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String AMR_REV_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String AMR_CHIN_DATA_FORMAT = "yyyy年MM月dd日";
    
    /**
     * 获取当前日期，yyyy-MM-dd格式
     * 
     * @return
     */
    public static String getToday2() {
        DateTime oDate = new DateTime(System.currentTimeMillis());
        return oDate.toString(AMR_NOMAL_DATE_FORMAT2);
    }

    public static String formatDate(Date date) {
        DateTime oDate = new DateTime(date.getTime());
        return oDate.toString(AMR_NOMAL_DATE_FORMAT);
    }
    public static String formatDateTime(Date date) {
        DateTime oDate = new DateTime(date.getTime());
        return oDate.toString(AMR_NOMAL_DATE_TIME_FORMAT);
    }
    public static String formatDateTime2(Date date) {
        DateTime oDate = new DateTime(date.getTime());
        return oDate.toString(AMR_REV_DATE_TIME_FORMAT);
    }
    
    public static String formatDate(Date date,String format) {
        DateTime oDate = new DateTime(date.getTime());
        return oDate.toString(format);
    }
    
    public static long getRangeMillis(String start,String end) {
    	DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(AMR_REV_DATE_TIME_FORMAT);
        DateTime st = dateTimeFormatter.parseDateTime(start);
        DateTime et = dateTimeFormatter.parseDateTime(end);
        return et.getMillis()-st.getMillis();
    }
    
    public static DateTime toFormatDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(AMR_NOMAL_DATE_TIME_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime;
    }

    /**
     * yyyy/MM/dd转YYYY-MM-DD
     * @param date
     * @return
     * @throws ParseException
     */
    public static String DateToDate(String date) throws ParseException{
        if(date.equals("")){
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");   
        Date dates = formatter.parse(date);
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        String a = formatter1.format(dates);
        return a;
    }
    
    /**
     * YYYY-MM-DD转yyyy/MM/dd
     * @param date
     * @return
     * @throws ParseException
     */
    public static String DateToDate2(String date) throws ParseException{
        if(date.equals("")){
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
        Date dates = formatter.parse(date);
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd");
        String a = formatter1.format(dates);
        return a;
    }
    
    /**
     *yyyy/MM/dd HH:mm:ss 转YYYY-MM-DD HH:mm:ss
     * @param date
     * @return
     * @throws ParseException
     */
    public static String TimeToTime(String date){
        if(date.equals("")){
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(AMR_NOMAL_DATE_TIME_FORMAT);   
        Date dates;
        String a="";
        try {
            dates = formatter.parse(date);
            a  = date.replaceAll("/", "-");
            
        } catch (ParseException e) {
            String b =date+" 00:00:00";
            a  = b.replaceAll("/", "-");
            return a;
        }
      
        return a;
    }
    
    
    public static String castDateFormat(String date) throws ParseException{
        Date dateTemp = new SimpleDateFormat(AMR_NOMAL_DATE_FORMAT).parse(date);
        date = new SimpleDateFormat(AMR_CHIN_DATA_FORMAT).format(dateTemp);
        return date;
    }
    
    /**
     *  获取指定日期之后指定类型指定数值后的日期
     * @param dateStr 指定日期
     * @param format 指定日期format
     * @param num 指定数值
     * @param type 指定类型（年，月，日，等）
     * @return
     * @throws ParseException
     */
    public static String getDateAdd(String dateStr,String format,int num,int type) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date d = dateFormat.parse(dateStr);
        Calendar ca = Calendar.getInstance();
        ca.setTime(d);
        ca.add(type, num);
        Date date = ca.getTime();
        return dateFormat.format(date);
    }
    
    /**
     * Date类型转为指定格式的String类型
     * 
     * @param source
     * @param pattern
     * @return
     */
    public static String DateToString(Date source, String pattern) {
      simpleDateFormat = new SimpleDateFormat(pattern);
      return simpleDateFormat.format(source);
    }
   
    /**
     * 
     * unix时间戳转为指定格式的String类型
     * 
     * 
     * System.currentTimeMillis()获得的是是从1970年1月1日开始所经过的毫秒数
     * unix时间戳:是从1970年1月1日（UTC/GMT的午夜）开始所经过的秒数,不考虑闰秒
     * 
     * @param source
     * @param pattern
     * @return
     */
    public static String timeStampToString(long source) {
        return timeStampToString(source,AMR_REV_DATE_TIME_FORMAT);
  }
    
    public static String timeStampToString(long source, String pattern) {
      simpleDateFormat = new SimpleDateFormat(pattern);
      Date date = new Date(source * 1000);
      return simpleDateFormat.format(date);
    }
   
    /**
     * 将日期转换为时间戳(unix时间戳,单位秒)
     * 
     * @param date
     * @return
     */
    public static long dateToTimeStamp(Date date) {
      Timestamp timestamp = new Timestamp(date.getTime());
      return timestamp.getTime() / 1000;
    }
   
    /**
     * 获得当前时间对应的指定格式
     * 
     * @param pattern
     * @return
     */
    public static String currentFormatDate(String pattern) {
      simpleDateFormat = new SimpleDateFormat(pattern);
      return simpleDateFormat.format(new Date());
   
    }
   
    /**
     * 获得当前unix时间戳(单位秒)
     * 
     * @return 当前unix时间戳
     */
    public static long currentTimeStamp() {
      return System.currentTimeMillis() / 1000;
    }
    
}
